
package com.lzx.iteam.util;

import android.content.*;
import android.content.Entity.NamedContentValues;
import android.provider.ContactsContract.CommonDataKinds.*;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.RawContactsEntity;

import java.util.*;

public final class ContactsSend {

    private static final String TAG = "ContactsSync";

    public String mContactName;

    public Dictionary<String, String> userNameCard = new Hashtable<String, String>();

    // public Dictionary<String, String> userNameCardResult = new
    // Hashtable<String, String>();

    public static Dictionary<String, String> address = new Hashtable<String, String>();

    /*private static final int MAX_IM_ROWS = 3;

    private static final int MAX_EMAIL_ROWS = 3;

    private static final int[] EMAIL_TAGS = new int[] {
            Tags.CONTACTS_EMAIL1_ADDRESS, Tags.CONTACTS_EMAIL2_ADDRESS,
            Tags.CONTACTS_EMAIL3_ADDRESS
    };

    private static final int MAX_PHONE_ROWS = 2;

    private static final String COMMON_DATA_ROW = Im.DATA; // Could have been
                                                           // Email.DATA, etc.

    private static ContactsSend mInstance;

    private static final String COMMON_TYPE_ROW = Phone.TYPE; // Could have been
                                                              // any typed row

    private static final int[] WORK_PHONE_TAGS = new int[] {
            Tags.CONTACTS_BUSINESS_TELEPHONE_NUMBER, Tags.CONTACTS_BUSINESS2_TELEPHONE_NUMBER
    };

    private static final int[] HOME_ADDRESS_TAGS = new int[] {
            Tags.CONTACTS_HOME_ADDRESS_CITY, Tags.CONTACTS_HOME_ADDRESS_COUNTRY,
            Tags.CONTACTS_HOME_ADDRESS_POSTAL_CODE, Tags.CONTACTS_HOME_ADDRESS_STATE,
            Tags.CONTACTS_HOME_ADDRESS_STREET
    };

    private static final int[] WORK_ADDRESS_TAGS = new int[] {
            Tags.CONTACTS_BUSINESS_ADDRESS_CITY, Tags.CONTACTS_BUSINESS_ADDRESS_COUNTRY,
            Tags.CONTACTS_BUSINESS_ADDRESS_POSTAL_CODE, Tags.CONTACTS_BUSINESS_ADDRESS_STATE,
            Tags.CONTACTS_BUSINESS_ADDRESS_STREET
    };

    private static final int[] OTHER_ADDRESS_TAGS = new int[] {
            Tags.CONTACTS_HOME_ADDRESS_CITY, Tags.CONTACTS_OTHER_ADDRESS_COUNTRY,
            Tags.CONTACTS_OTHER_ADDRESS_POSTAL_CODE, Tags.CONTACTS_OTHER_ADDRESS_STATE,
            Tags.CONTACTS_OTHER_ADDRESS_STREET
    };

    private static final int[] HOME_PHONE_TAGS = new int[] {
            Tags.CONTACTS_HOME_TELEPHONE_NUMBER, Tags.CONTACTS_HOME2_TELEPHONE_NUMBER
    };

    private static final String[] allKeys = new String[] {
            "dataversion", "firstname", "lastname", "middlename", "nickname", "note", "jobtitle",
            "department", "organization", "demails", "durls", "dphones", "daddresses",
            // "phonenumbers", "phonenumberlabels", "url", "email",
            // "emailaddress", "aaddress", "address",
            "image",
    };*/
    
    private static ContactsSend mInstance;

    public static ContactsSend getInstance() {
        if (mInstance == null) {
            mInstance = new ContactsSend();
        }
        return mInstance;
    }

    public static String getFormatedContact(Context context, long contactID) {

        // First, let's find Contacts that have changed.
        ContentResolver cr = context.getContentResolver();
        String mSelection = RawContacts.CONTACT_ID + "=" + contactID;
        EntityIterator ei = RawContacts.newEntityIterator(cr.query(RawContactsEntity.CONTENT_URI,
                null, mSelection, null, null));
        
        
        StringBuffer result = new StringBuffer();
        try {
            boolean first = true;
            while (ei.hasNext()) {
                Entity entity = ei.next();
                if (first) {
                    first = false;
                }

                StringBuffer contactName = new StringBuffer();
                StringBuffer phoneNumber = new StringBuffer();
                StringBuffer emailAddress = new StringBuffer();
                StringBuffer organization = new StringBuffer();
                StringBuffer website = new StringBuffer();
                for (NamedContentValues ncv : entity.getSubValues()) {
                    ContentValues cv = ncv.values;
                    String mimeType = cv.getAsString(Data.MIMETYPE);
                    if (StringUtil.isEmpty(mimeType)) continue;
                    if (mimeType.equals(StructuredName.CONTENT_ITEM_TYPE)) {
                        String name = cv.getAsString(StructuredName.DISPLAY_NAME);
                        if (!StringUtil.isEmpty(name)) {
                            contactName.append("姓名:");
                            contactName.append(name);
                            contactName.append("\n");
                        }
                    } else if (mimeType.equals(Email.CONTENT_ITEM_TYPE)) {
                        String email = cv.getAsString(Email.DATA);
                        if (!StringUtil.isEmpty(email)) {
                            emailAddress.append("邮件:");
                            emailAddress.append(email);
                            emailAddress.append("\n");
                        }
                    } else if (mimeType.equals(Nickname.CONTENT_ITEM_TYPE)) {
                    } else if (mimeType.equals(Website.CONTENT_ITEM_TYPE)) {
                    } else if (mimeType.equals(Phone.CONTENT_ITEM_TYPE)) {
                        String phone = cv.getAsString(Phone.NUMBER);
                        if (!StringUtil.isEmpty(phone)) {
                            phoneNumber.append("电话:");
                            phoneNumber.append(phone);
                            phoneNumber.append("\n");
                        }
                    } else if (mimeType.equals(StructuredPostal.CONTENT_ITEM_TYPE)) {
                    } else if (mimeType.equals(Organization.CONTENT_ITEM_TYPE)) {
                        String jobtitle = cv.getAsString(Organization.TITLE);
                        if (!StringUtil.isEmpty(jobtitle)) {
                            organization.append("职务:");
                            organization.append(jobtitle);
                            organization.append("\n");
                        }
                        String company = cv.getAsString(Organization.COMPANY);
                        if (!StringUtil.isEmpty(company)) {
                            organization.append("企业:");
                            organization.append(company);
                            organization.append("\n");
                        }
                        String department = cv.getAsString(Organization.DEPARTMENT);
                        if (!StringUtil.isEmpty(department)) {
                            organization.append("部门:");
                            organization.append(department);
                            organization.append("\n");
                        }
                    } else if (mimeType.equals(Note.CONTENT_ITEM_TYPE)) {
                    } else if (mimeType.equals(Photo.CONTENT_ITEM_TYPE)) {
                    } else if (mimeType.equals(Website.CONTENT_ITEM_TYPE)){
                        String url = cv.getAsString(Website.URL);
                        if(!StringUtil.isEmpty(url)){
                            website.append("主页:");
                            website.append(url);
                            website.append("\n");
                        }
                    }else{
                        
                    }
                }
                result.append(contactName.toString());
                if (organization.length() > 0) {
                    result.append(organization.toString());
                }
                if (phoneNumber.length() > 0) {
                    result.append(phoneNumber.toString());
                }
                if (emailAddress.length() > 0) {
                    result.append(emailAddress.toString());
                }
                if(website.length()>0){
                    result.append(website.toString());
                }
                
                if(result.length() > 0){
                    result.append(Constants.NAME_FLAG);
                }
            }
        } finally {
            ei.close();
        }

        return result.toString();
        
    }













}
